package com.supconit.its.domaingenerator;

import com.supconit.its.environment.Environment;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import static com.supconit.its.environment.Environment.entityUpCase;
import static com.supconit.its.environment.Environment.separator;

/**
 * <p>
 * 默认 domain 生成模板
 * </p>
 *
 * @author 史金涛
 * @date 2020/8/8 10:45
 */
@Deprecated
public class DefaultDomainTemplate extends DomainTemplate {

    @Override
    protected void createDirs() {
        // dto
        String dtoPath = Environment.DOMAIN_PATH + "dto" + separator;
        mkdir(dtoPath);
        // qo
        String qoPath = Environment.DOMAIN_PATH + "qo" + separator;
        mkdir(qoPath);
        // bo
        String boPath = Environment.DOMAIN_PATH + "bo" + separator;
        mkdir(boPath);
    }

    @Override
    protected void createListDTO() throws IOException {
        String poFile = Environment.DOMAIN_PATH + "po" + separator + entityUpCase + "PO.java";
        String dtoPath = Environment.DOMAIN_PATH + "dto" + separator + entityUpCase + "ListDTO.java";
        RandomAccessFile rdr = new RandomAccessFile(poFile, "r");
        List<String> lines = new ArrayList<>();
        // 按行读取字符串
        String str;
        boolean jsonFormatImported = false;
        while ((str = rdr.readLine()) != null) {
            if (str.contains("package")) {
                str = str.replace(".po", ".dto");
            }
            if (str.contains("import com.baomidou.mybatisplus.annotation.TableName;") ||
                    str.contains("import com.baomidou.mybatisplus.annotation.TableField;") ||
                    str.contains("import com.baomidou.mybatisplus.annotation.IdType;") ||
                    str.contains("import com.baomidou.mybatisplus.annotation.TableId;") ||
                    str.contains("import java.io.Serializable;") ||
                    str.contains("@TableName") ||
                    str.contains("@TableId") ||
                    str.contains("@TableField") ||
                    str.contains("serialVersionUID")) {
                continue;
            }
            if (str.contains("@ApiModel")) {
                str = str.replace("PO", "ListDTO");
            }
            if (str.contains("class")) {
                str = str.replace("PO", "ListDTO");
            }
            if (str.contains("private LocalDateTime")) {
                if (!jsonFormatImported) {
                    lines.add(2, "import com.fasterxml.jackson.annotation.JsonFormat;");
                    jsonFormatImported = true;
                }
                str = "@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")\r\n" + str;
            }
            str = str.replace("implements Serializable", "");
            lines.add(str);
        }
        writeLines(dtoPath, lines);
    }

    @Override
    protected void createEntityDTO() throws IOException {
        String listDtoPath = Environment.DOMAIN_PATH + "dto" + separator + entityUpCase + "ListDTO.java";
        String dtoPath = Environment.DOMAIN_PATH + "dto" + separator + entityUpCase + "DTO.java";
        RandomAccessFile rdr = new RandomAccessFile(listDtoPath, "r");
        List<String> lines = new ArrayList<>();
        // 按行读取字符串
        String str;
        while ((str = rdr.readLine()) != null) {
            if (str.contains("@ApiModel")) {
                str = str.replace("ListDTO", "DTO");
            }
            if (str.contains("class")) {
                str = str.replace("ListDTO", "DTO");
            }
            lines.add(str);
        }
        writeLines(dtoPath, lines);
    }

    @Override
    protected void createPageDTO() throws IOException {
        String dtoPath = Environment.DOMAIN_PATH + "dto" + separator + entityUpCase + "ListDTO.java";
        String pageDtoPath = Environment.DOMAIN_PATH + "dto" + separator + entityUpCase + "PageDTO.java";
        RandomAccessFile rdr = new RandomAccessFile(dtoPath, "r");
        List<String> lines = new ArrayList<>();
        // 按行读取字符串
        String str;
        while ((str = rdr.readLine()) != null) {
            if (str.contains("@ApiModel")) {
                str = str.replace("ListDTO", "PageDTO");
            }
            if (str.contains("class")) {
                str = str.replace("ListDTO", "PageDTO");
            }
            lines.add(str);
        }
        writeLines(pageDtoPath, lines);
    }

    @Override
    protected void createAddBO() throws IOException {
        String poFile = Environment.DOMAIN_PATH + "po" + separator + entityUpCase + "PO.java";
        String saveBoPath = Environment.DOMAIN_PATH + "bo" + separator + entityUpCase + "AddBO.java";
        RandomAccessFile rdr = new RandomAccessFile(poFile, "r");
        List<String> lines = new ArrayList<>();
        // 按行读取字符串
        String str;
        boolean isId = false;
        boolean jsonFormatImported = false;
        while ((str = rdr.readLine()) != null) {
            if (str.contains("import com.baomidou.mybatisplus.annotation.TableName;") ||
                    str.contains("import com.baomidou.mybatisplus.annotation.TableField;") ||
                    str.contains("import com.baomidou.mybatisplus.annotation.IdType;") ||
                    str.contains("import com.baomidou.mybatisplus.annotation.TableId;") ||
                    str.contains("import java.io.Serializable;") ||
                    str.contains("@TableName") ||
                    str.contains("@TableField") ||
                    str.contains("serialVersionUID")) {
                continue;
            }
            if (str.contains("private LocalDateTime")) {
                if (!jsonFormatImported) {
                    lines.add(2, "import com.fasterxml.jackson.annotation.JsonFormat;");
                    jsonFormatImported = true;
                }
                str = "@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")\r\n" + str;
            }
            if (isId) {
                isId = false;
                continue;
            }
            if (str.contains("@TableId")) {
                int lastIndex = lines.size() - 1;
                String last = lines.get(lastIndex);
                if (last.contains("@ApiModelProperty")) {
                    lines.remove(lastIndex);
                }
                isId = true;
                continue;
            }
            if (str.contains("package")) {
                str = str.replace(".po", ".bo");
            }
            if (str.contains("@ApiModel")) {
                str = str.replace("PO", "AddBO");
            }
            if (str.contains("class")) {
                str = str.replace("PO", "AddBO");
            }
            str = str.replace("implements Serializable", "");
            lines.add(str);
        }
        writeLines(saveBoPath, lines);
    }

    @Override
    protected void createEditBO() throws IOException {
        String dtoPath = Environment.DOMAIN_PATH + "dto" + separator + entityUpCase + "ListDTO.java";
        String editBoPath = Environment.DOMAIN_PATH + "bo" + separator + entityUpCase + "EditBO.java";
        RandomAccessFile rdr = new RandomAccessFile(dtoPath, "r");
        List<String> lines = new ArrayList<>();
        // 按行读取字符串
        String str;
        while ((str = rdr.readLine()) != null) {
            if (str.contains("package")) {
                str = str.replace(".dto", ".bo");
            }
            if (str.contains("@ApiModel")) {
                str = str.replace("ListDTO", "EditBO");
            }
            if (str.contains("class")) {
                str = str.replace("ListDTO", "EditBO");
            }
            lines.add(str);
        }
        writeLines(editBoPath, lines);
    }

    @Override
    protected void createEditDTO() throws IOException {
        String editBoPath = Environment.DOMAIN_PATH + "bo" + separator + entityUpCase + "EditBO.java";
        String editDtoPath = Environment.DOMAIN_PATH + "dto" + separator + entityUpCase + "EditDTO.java";
        RandomAccessFile rdr = new RandomAccessFile(editBoPath, "r");
        List<String> lines = new ArrayList<>();
        // 按行读取字符串
        String str;
        while ((str = rdr.readLine()) != null) {
            if (str.contains("package")) {
                str = str.replace(".bo", ".dto");
            }
            if (str.contains("@ApiModel")) {
                str = str.replace("EditBO", "EditDTO");
            }
            if (str.contains("class")) {
                str = str.replace("EditBO", "EditDTO");
            }

            lines.add(str);
        }
        writeLines(editDtoPath, lines);
    }

    @Override
    protected void createListQO() throws IOException {
        String dtoPath = Environment.DOMAIN_PATH + "dto" + separator + entityUpCase + "ListDTO.java";
        String entityQoPath = Environment.DOMAIN_PATH + "qo" + separator + entityUpCase + "ListQO.java";
        RandomAccessFile rdr = new RandomAccessFile(dtoPath, "r");
        List<String> lines = new ArrayList<>();
        // 按行读取字符串
        String str;
        boolean dateFormatImported = false;
        while ((str = rdr.readLine()) != null) {
            if (str.contains("import com.fasterxml.jackson.annotation.JsonFormat;") ||
                    str.contains("@JsonFormat")) {
                continue;
            }
            if (str.contains("package")) {
                str = str.replace(".dto", ".qo");
            }
            if (str.contains("@ApiModel")) {
                str = str.replace("ListDTO", "ListQO");
            }
            if (str.contains("class")) {
                str = str.replace("ListDTO", "ListQO");
            }
            if (str.contains("private LocalDateTime")) {
                if (!dateFormatImported) {
                    lines.add(2, "import org.springframework.format.annotation.DateTimeFormat;");
                    dateFormatImported = true;
                }
                str = "@DateTimeFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")\r\n" + str;
            }
            lines.add(str);
        }
        writeLines(entityQoPath, lines);
    }

    @Override
    protected void createEntityQO() throws IOException {
        String listQoPath = Environment.DOMAIN_PATH + "qo" + separator + entityUpCase + "ListQO.java";
        String qoPath = Environment.DOMAIN_PATH + "qo" + separator + entityUpCase + "QO.java";
        RandomAccessFile rdr = new RandomAccessFile(listQoPath, "r");
        List<String> lines = new ArrayList<>();
        // 按行读取字符串
        String str;
        while ((str = rdr.readLine()) != null) {
            if (str.contains("@ApiModel")) {
                str = str.replace("ListQO", "QO");
            }
            if (str.contains("class")) {
                str = str.replace("ListQO", "QO");
            }
            lines.add(str);
        }
        writeLines(qoPath, lines);
    }

    @Override
    protected void createPageQO() throws IOException {
        String qoPath = Environment.DOMAIN_PATH + "qo" + separator + entityUpCase + "ListQO.java";
        String pageQoPath = Environment.DOMAIN_PATH + "qo" + separator + entityUpCase + "PageQO.java";
        RandomAccessFile rdr = new RandomAccessFile(qoPath, "r");
        List<String> lines = new ArrayList<>();
        // 按行读取字符串
        String str;
        while ((str = rdr.readLine()) != null) {
            if (str.contains("@ApiModel")) {
                str = str.replace("ListQO", "PageQO");
            }
            if (str.contains("class")) {
                str = str.replace("ListQO", "PageQO");
            }
            if (str.contains("}")) {
                String context = "@ApiModelProperty(value = \"current page\", example = \"1\", required = true)\r\n" +
                        "    private Long current;\r\n" +
                        "\r\n" +
                        "    @ApiModelProperty(value = \"page size\", example = \"20\", required = true)\r\n" +
                        "    private Long size;\r\n";
                lines.add(context);
            }
            lines.add(str);
        }
        writeLines(pageQoPath, lines);
    }
}
